package com.qufadai.framework.common.utils.json.serializer;

import java.io.IOException;

import com.qufadai.framework.common.utils.json.JsonStringSymbol;
import com.qufadai.framework.common.utils.json.Serializer;
import com.qufadai.framework.common.utils.json.support.JsonStringWriter;

public class ArraySerializer implements Serializer {

	@Override
	public void convertTo(JsonStringWriter writer, Object obj)
			throws IOException {
		Object[] objArray = (Object[]) obj;
		int iMax = objArray.length - 1;
		if (iMax == -1) {
			writer.write(JsonStringSymbol.EMPTY_ARRAY);
			return;
		}

		writer.append(JsonStringSymbol.ARRAY_PRE);
		for (int i = 0;; i++) {
			SerialStateMachine.toJson(objArray[i], writer);
			if (i == iMax) {
				writer.append(JsonStringSymbol.ARRAY_SUF);
				return;
			}
			writer.append(JsonStringSymbol.SEPARATOR);
		}
	}

}
